document.write("<script type=\"text/javascript\" src=\"/static/js/jquery-1.10.2.min.js\"></script>")
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/static/layui/css/layui.css\">")
document.write("<script type=\"text/javascript\" src=\"/static/layui/layui.js\"></script>")
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/static/css/toastr/toastr.min.css\">")
document.write("<script type=\"text/javascript\" src=\"/static/js/toastr/toastr.min.js\"></script>")
document.write("<script type=\"text/javascript\" src=\"/static/js/vue/vue.min.js\"></script>")
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/static/css/sweetalert/sweetalert.css\">")
document.write("<script type=\"text/javascript\" src=\"/static/js/sweetalert/sweetalert.min.js\"></script>")
document.write("<script type=\"text/javascript\" src=\"/common/jsencrypt.min.js\"></script>")


var BaseUrl = 'http://182.92.69.172:8081/';
// var NGINX_SERVER = 'http://182.92.69.172/';


// var BaseUrl = 'http://localhost:8081/';
var NGINX_SERVER = 'http://localhost/';
function toTopFun() {//回到顶部功能
    var $win=$(window),
        $toUp = $('#toUp'),
        $bodyHtml = $('body,html'),
        scrollTimer=null;
    //在一定的时间之内，只执行一次scroll事件函数
    if (scrollTimer) {
        clearTimeout(scrollTimer);
    }
    scrollTimer = setTimeout(function(){
        $win.off('scroll').on('scroll',function(){
            var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
            scrolltop>0?$toUp.addClass('show'):$toUp.removeClass('show');
        });
    },400);
    $toUp.off('click').on('click',function(){
        $bodyHtml.animate({scrollTop:0},200);
    });
};
(function() {
setTimeout(function(){
    // toastr 配置
    toastr.options = {
        "closeButton": false,
        "debug": false,
        "progressBar": false,
        "positionClass": "toast-top-center",
        "onclick": null,
        "showDuration": "200",
        "hideDuration": "200",
        "timeOut": "3000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }
},100)
    if (!sessionStorage.length) {
        // 这个调用能触发目标事件，从而达到共享数据的目的
        localStorage.setItem('getSessionStorage', Date.now());
    };

    // 该事件是核心
    window.addEventListener('storage', function(event) {
        if(localStorage.getItem("logout")=="true"){
            return;
        }
        if (event.key == 'getSessionStorage') {
            // 已存在的标签页会收到这个事件
            localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
            localStorage.removeItem('sessionStorage');

        } else if (event.key == 'sessionStorage' && !sessionStorage.length) {
            // 新开启的标签页会收到这个事件
            var data = JSON.parse(event.newValue),
                    value;

            for (key in data) {
                sessionStorage.setItem(key, data[key]);
            }
        }
    });
})();


function getUnreadCount(callback){
    $.ajax({
        xhrFields: {
            withCredentials: true
        },
        type: "GET",
        url: BaseUrl + 'api/user/getUnreadCount',
        data: {},
        dataType: 'json',
        success: function(res) {
            if(res.code==200){
                callback(res.data);
            }
        },
        fail: function(res){
            toastr.error("系统异常，请稍候再试！");
        }
    });
}

function getDataByGet(url,data,successCallback){
    $.ajax({
        xhrFields: {
            withCredentials: true
        },
        type: "GET",
        url: BaseUrl + url,
        data: data,
        dataType: 'json',
        success: function(res) {
            if(res.msg=='请先登录！'){
                toastr.error("登录信息失效，请重新登录！");
                localStorage.setItem("logout",true);
                sessionStorage.removeItem("userInfo");
                setTimeout(function () {
                    window.location.href="/login.html";
                },1500)
            }else{
                successCallback(res);
            }
        },
        fail: function(res){
            toastr.error("系统异常，请稍候再试！");
        }
    });
}

function getDataByPost(url,data,successCallback){
    $.ajax({
        headers:{
            "content-Type":"application/json;charset=UTF-8",
            "Accept":"application/json;charset=UTF-8"
        },
        xhrFields: {
            withCredentials: true
        },
        type: "POST",
        url: BaseUrl + url,
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(res) {
            if(res.msg=='请先登录！'){
                toastr.error("登录信息失效，请重新登录！");
                localStorage.setItem("logout",true);
                sessionStorage.removeItem("userInfo");
                setTimeout(function () {
                    window.location.href="/login.html";
                },1500)
            }else{
                successCallback(res);
            }
        },
        fail: function(res){
            toastr.error("系统异常，请稍候再试！");
        }
    });
}

function postDataByFormData(url,formData,successCallback){
    $.ajax({
        xhrFields: {
            withCredentials: true
        },
        type: "POST",
        url: BaseUrl + url,
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(res) {
            if(res.msg=='请先登录！'){
                toastr.error("登录信息失效，请重新登录！");
                localStorage.setItem("logout",true);
                sessionStorage.removeItem("userInfo");
                setTimeout(function () {
                    window.location.href="/login.html";
                },1500)
            }else{
                successCallback(res);
            }
        },
        fail: function(res){
            toastr.error("系统异常，请稍候再试！");
        }
    });
}

//根据名称获取url中的参数值
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); // 匹配目标参数
    if (r != null)
        return decodeURI(r[2]);
    return null; //返回参数值
};

function logout(){
    swal({
        title: "您确定要退出登录吗",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确定",
        closeOnConfirm: false
    }, function() {
        getDataByGet('api/user/logout',{},res=>{
            if(res.code==200){
                toastr.success("退出登录成功！")
                sessionStorage.removeItem("userInfo")
                localStorage.setItem("logout",true);
                setTimeout(function(){
                    window.location.href="/login.html"
                },1500)
            }else{
                toastr.error(res.msg);
            }
        })
    })
    
}

function encodeString(str,callback){
    var encryptor = new JSEncrypt();

    $.ajax({
        xhrFields:{
            withCredentials:true
        },
        type:"GET",
        url:BaseUrl+'admin/getPublicKey',
        success: res=>{
            encryptor.setPublicKey(res);
            var password = encryptor.encrypt(str);
            callback(password);
        }
    });
}